package cn.fzkj.project.common.controller;

import cn.fzkj.common.ResultVO;
import cn.fzkj.common.constant.Constants;
import cn.fzkj.common.utils.IdUtils;
import cn.fzkj.common.utils.StringUtil;
import cn.fzkj.common.utils.VerifyCodeUtils;
import cn.fzkj.common.utils.sign.Base64;
import cn.fzkj.framework.messagequece.MessageType;
import cn.fzkj.framework.messagequece.service.MessageService;
import cn.fzkj.framework.redis.utils.RedisUtil;
import cn.fzkj.project.common.service.ICommonService;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("common")
public class CommonController {

    @RequestMapping("encode")
    public String encode(){
//        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//        String encode = encoder.encode("123");
        return "成功了";
    }

    @Autowired
    private ICommonService commonService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private MessageService messageService;

    /** 拿到背景图片 */
    @RequestMapping(value = "getImg", method = RequestMethod.GET)
    public ResultVO getImg(){
        String data = (String)redisUtil.get(Constants.IMG_PREFIX);
        if (StringUtil.isEmpty(data)){
            data = commonService.getImgByPy();
        }
        messageService.sendMsg2Redis(MessageType.REDIS_GET_IMG, "");
        return ResultVO.success(data);
    }

    /** 拿到推荐句子 */
    @RequestMapping(value = "getWords", method = RequestMethod.GET)
    public ResultVO getWords(){
        return ResultVO.success("拿到句子的方法调用成功");
    }

    /** 返回验证码 */
    @RequestMapping("captchaImage")
    public ResultVO captcha(HttpResponse response) throws IOException {
        // 生成随机字串
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        // 唯一标识
        String uuid = IdUtils.simpleUUID();
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;

        redisUtil.setCacheObject(verifyKey, verifyCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        // 生成图片
        int w = 111, h = 36;
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        VerifyCodeUtils.outputImage(w, h, stream, verifyCode);
        try {
            ResultVO ajax = ResultVO.success();
            Map map = new HashMap();
            map.put("uuid", uuid);
            map.put("img", Base64.encode(stream.toByteArray()));
            ajax.setData(map);
            return ajax;
        } catch (Exception e)
        {
            e.printStackTrace();
            return ResultVO.failure(e.getMessage());
        } finally
        {
            stream.close();
        }
    }

}
